package cece.service.impl;

import cece.entity.Item;
import cece.entity.ItemNew;
import cece.entity.QueryResult;
import cece.mapper.ItemHotRepository;
import cece.mapper.ItemRepository;
import cece.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Auther Programmer
 * @Date 2020-05-28 14:20
 */
@Service
public class ItemServiceImpl implements ItemService {
	@Autowired
	private ItemRepository itemRepository;

	@Autowired
	private ItemHotRepository itemHotRepository;

	@Override
	public List<Item> findAll() {
		return itemRepository.findAll();
	}

	@Override
	public List<ItemNew> findHot() {
		return itemHotRepository.findAll();
	}

	@Override
	public QueryResult findByPage(Integer page, Integer size) {
		if (page <= 0) {
			page = 1;
		}
		if (size <= 0) {
			size = 4;
		}
		Pageable pageRequest = PageRequest.of(page-1, size);
		QueryResult queryResult = new QueryResult();
		Page<Item> all = itemRepository.findAll(pageRequest);
		List<Item> content = all.getContent();
		long totalElements = all.getTotalElements();
		queryResult.setList(content);
		queryResult.setTotal(totalElements);
		return queryResult;
	}
}
